import urllib.request
import urllib.parse

# 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
# https UA校验反爬，需封装request headers
base_url = 'https://www.baidu.com/s?'
# get请求方式，对字符串编码
url = base_url + 'wd=' + urllib.parse.quote('周杰伦')
print('url>>>', url)
# get请求方式，对多个参数进行编码，类似https://www.baidu.com/s?wd=蒜泥白肉&location=四川
data = {
    'wd':'周杰伦',
    'sex':'男'
}
url1 = base_url + urllib.parse.urlencode(data)
print('url1>>>', url1)

headers = {
    'User-Agent':
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}

request = urllib.request.Request(url=url, headers=headers)

# proxies = {'http':'222.89.70.65:25002'}
# handler = urllib.request.ProxyHandler(proxies=proxies)
# opener = urllib.request.build_opener(handler)
# response = opener.open(request)
# content = response.read().decode('utf‐8')
# print(content)

response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
print(content)
